using Microsoft.Extensions.Configuration;

namespace sgf.server
{
    public class Config
    {
        private static IConfigurationRoot _config = null!;

        public static void Init(string fileName = "config.property")
        {
            // 使用自定义的配置源
            var builder = new ConfigurationBuilder();
            builder.Add(new PropertyConfigurationSource
                { filePath = $"../Config/{fileName}" });
            _config = builder.Build();
        }

        public static string Get(string key, string defaultValue = "")
        {
            return _config[key] ?? defaultValue;
        }

        public static int Get(string key, int defaultValue = 0)
        {
            return GetInt(key, defaultValue);
        }

        public static bool Get(string key, bool defaultValue = false)
        {
            return GetBool(key, defaultValue);
        }

        public static float Get(string key, float defaultValue = 0)
        {
            return GetFloat(key, defaultValue);
        }

        public static double Get(string key, double defaultValue = 0)
        {
            return GetDouble(key, defaultValue);
        }

        public static long Get(string key, long defaultValue = 0)
        {
            return GetLong(key, defaultValue);
        }

        public static string GetStr(string key, string defaultValue = "")
        {
            return _config[key] ?? defaultValue;
        }

        public static int GetInt(string key, int defaultValue = 0)
        {
            return int.TryParse(_config[key], out var value) ? value : defaultValue;
        }

        public static bool GetBool(string key, bool defaultValue = false)
        {
            return bool.TryParse(_config[key], out var value) ? value : defaultValue;
        }

        public static float GetFloat(string key, float defaultValue = 0)
        {
            return float.TryParse(_config[key], out var value) ? value : defaultValue;
        }

        public static double GetDouble(string key, double defaultValue = 0)
        {
            return double.TryParse(_config[key], out var value) ? value : defaultValue;
        }

        public static long GetLong(string key, long defaultValue = 0)
        {
            return long.TryParse(_config[key], out var value) ? value : defaultValue;
        }

        public static string[] GetArray(string key, string separator = ",")
        {
            return _config[key].Split(separator);
        }
    }
}